using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpriteAnimations.Graphics;
using SpriteAnimations.Graphics.Animations;

namespace Fruchtschlingel
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        //
        // Fields
        //
        private GraphicsDeviceManager m_GraphicsDevice;
        private ContentManager m_ContentManager;
        private SpriteBatch m_SpriteBatch;
        private Sprite xna3waySprite;
        private Sprite test;

        private Sprite m_Background;
        private Sprite m_Schloss;
        private Sprite m_Ameisen;
       
        private Sprite CollisionBig, CollisionSmall;
        MouseState mouseState;

        //
        // Constructors
        //
        public Game1()
        {
            m_GraphicsDevice = new GraphicsDeviceManager(this);
            m_ContentManager = new ContentManager(Services);

            m_GraphicsDevice.PreferredBackBufferWidth = 1024;
            m_GraphicsDevice.PreferredBackBufferHeight = 768;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            m_SpriteBatch = new SpriteBatch(m_GraphicsDevice.GraphicsDevice);

            base.Initialize();
        }

        //
        // Private/Protected Methods
        //
        protected override void Draw(GameTime gameTime)
        {
            m_GraphicsDevice.GraphicsDevice.Clear(Color.CornflowerBlue);

            m_SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            m_Background.Draw(m_SpriteBatch, gameTime);
            m_Schloss.Draw(m_SpriteBatch, gameTime);
            m_Ameisen.Draw(m_SpriteBatch, gameTime);
            
            xna3waySprite.Draw(m_SpriteBatch, gameTime);
            test.Draw(m_SpriteBatch, gameTime);
            CollisionBig.Draw(m_SpriteBatch, gameTime);
            CollisionSmall.Draw(m_SpriteBatch, gameTime);



            m_SpriteBatch.End();

            base.Draw(gameTime);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
            
            mouseState = Mouse.GetState();
            CollisionSmall.Parameters.Position = new Vector2(mouseState.X, mouseState.Y);

            // The time since Update was called last
            double elapsed = gameTime.ElapsedGameTime.TotalSeconds;

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                CollisionSmall.Parameters.Rotation -= (float)(elapsed * 5.0);
            }
            if (mouseState.RightButton == ButtonState.Pressed)
            {
                CollisionBig.Parameters.Rotation -= (float)(elapsed * 5.0);
            }
            if (mouseState.MiddleButton == ButtonState.Pressed)
            {
                CollisionSmall.Parameters.Rotation = 0.0f;
                CollisionBig.Parameters.Rotation = 0.0f;
            }

            if (Sprite.Intersects(CollisionSmall, CollisionBig))
            {
                CollisionSmall.Parameters.Tint = Color.Red;
                CollisionBig.Parameters.Tint = Color.Red;
            }
            else
            {
                CollisionSmall.Parameters.Tint = Color.White;
                CollisionBig.Parameters.Tint = Color.White;
            }
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                xna3waySprite = new Sprite(m_ContentManager, new Vector2(110, 500), "Data/Image/Windows");
                xna3waySprite.Animations.Add(new CompositeAnimation(new LerpColorAnimation(Color.Red, Color.Yellow, TimeSpan.FromSeconds(6))));

                test = new Sprite(m_ContentManager, new Vector2(400, 10), "Data/Image/ball");
                test.Animations.Add(new CompositeAnimation(new TextureAnimation(2, 4, 30, 30, TimeSpan.FromSeconds(0.4)),
                    new BlinkAnimation(TimeSpan.FromSeconds(1)), new PathAnimation(33.0f, new Vector2(93,456))));

                CollisionSmall = new Sprite(m_ContentManager, new Vector2(450, 300), "Data/Image/sprite2");
                CollisionSmall.Animations.Add(new CompositeAnimation(new BezierSplineAnimation()));

                CollisionBig = new Sprite(m_ContentManager, new Vector2(250, 300), "Data/Image/sprite1");

                m_Background = new Sprite(m_ContentManager, new Vector2(0, 0), "Data/Image/Hintergrund");
                m_Schloss = new Sprite(m_ContentManager, new Vector2(1, 325), "Data/Image/schloss");
                m_Schloss.Animations.Add(new CompositeAnimation(new TextureAnimation(1, 2, 230, 174, TimeSpan.FromSeconds(2)),
                    new LerpColorAnimation(Color.Gray, Color.BurlyWood,TimeSpan.FromSeconds(15))));
                m_Ameisen = new Sprite(m_ContentManager, new Vector2(480, 335), "Data/Image/ameisen");
                m_Ameisen.Animations.Add(new TextureAnimation(1, 2, 64, 240, TimeSpan.FromSeconds(0.2)));

            }
        }
    }
}